from IPython.display import display
from bokeh.plotting import output_notebook
from bokeh.plotting import show
import geopandas as gpd
from shapely.geometry import Point
from shapely.ops import linemerge
from easy_map_bokeh import EasyMapBokeh
from osmgt import OsmGt
from graph_tool.topology import shortest_path
output_notebook()
location = "Roanne"
%%time
pois_gdf = OsmGt.pois_from_location(location).get_gdf()
display(pois_gdf.head(2))
2020-10-01 22:08:38 - OsmGtPoi - INFO : From location: Roanne 2020-10-01 22:08:38 - OsmGtPoi - INFO : Loading data... 2020-10-01 22:08:38 - OsmGtPoi - INFO : NominatimApi: Query 200:OK in 0.37 sec. 2020-10-01 22:08:40 - OsmGtPoi - INFO : OverpassApi: Query 200:OK in 1.29 sec. 2020-10-01 22:08:40 - OsmGtPoi - INFO : Formating data 2020-10-01 22:08:40 - OsmGtPoi - INFO : Prepare GeoDataframe 2020-10-01 22:08:40 - OsmGtPoi - INFO : GeoDataframe Ready
| addr:postcode | amenity | atm | change_machine | name | operator | phone | ref:FR:LaPoste | source | stamping_machine | ... | ref:EU:EVSE | socket:type2 | socket:typee | bar | covered | social_facility:for | type:FR:FINESS | service:bicycle:Bicycle_Sales_and_Service | service:bicycle:repair | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 42300 | post_office | yes | yes | Roanne Principal | La Poste | 3631 | 07916A | data.gouv.fr:LaPoste - 10/2012 | yes | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | POINT (4.07225 46.04071) |
| 1 | NaN | place_of_worship | NaN | NaN | Chapelle Jean Puy | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | POINT (4.07073 46.03766) |
2 rows × 122 columns
CPU times: user 81 ms, sys: 16.9 ms, total: 97.9 ms Wall time: 1.73 s
%%time
roads_initialized = OsmGt.roads_from_location(
location,
mode="vehicle",
additional_nodes=pois_gdf
)
roads_gdf = roads_initialized.get_gdf()
display(roads_gdf.head(2))
2020-10-01 22:08:40 - OsmGtRoads - INFO : From location: Roanne 2020-10-01 22:08:40 - OsmGtRoads - INFO : Loading data... 2020-10-01 22:08:40 - OsmGtRoads - INFO : NominatimApi: Query 200:OK in 0.37 sec. 2020-10-01 22:08:43 - OsmGtRoads - INFO : OverpassApi: Query 200:OK in 2.56 sec. 2020-10-01 22:08:43 - OsmGtRoads - INFO : Rebuild network data 2020-10-01 22:08:43 - OsmGtRoads - INFO : Network cleaning... 2020-10-01 22:08:43 - OsmGtRoads - INFO : Starting: Adding new nodes on the network 2020-10-01 22:08:43 - OsmGtRoads - INFO : Find nearest line for each node 2020-10-01 22:08:43 - OsmGtRoads - INFO : Split line 2020-10-01 22:08:43 - OsmGtRoads - INFO : Topology lines checker: to add: 184, to split: 182 2020-10-01 22:08:43 - OsmGtRoads - INFO : Starting: Find intersections 2020-10-01 22:08:43 - OsmGtRoads - INFO : Done: Find intersections 2020-10-01 22:08:43 - OsmGtRoads - INFO : Build lines 2020-10-01 22:08:43 - OsmGtRoads - INFO : Prepare GeoDataframe 2020-10-01 22:08:43 - OsmGtRoads - INFO : GeoDataframe Ready
| highway | lanes:forward | maxspeed | name | oneway | ref | id | osm_url | topo_uuid | topology | ... | horse | EntityHand | SubClasses | alt_name | turn:lanes | source:name | maxspeed:backward | maxspeed:forward | cycleway:right:oneway | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | primary | 2 | 50 | Rue de Charlieu | yes | D 482 | 24035569 | https://www.openstreetmap.org/way/24035569 | 1_forward | unchanged | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | LINESTRING (4.08544 46.05234, 4.08546 46.05248... |
| 1 | primary | NaN | 50 | Rue de Charlieu | yes | D 482 | 24035570 | https://www.openstreetmap.org/way/24035570 | 2_forward | unchanged | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | LINESTRING (4.09087 46.06639, 4.09084 46.06622... |
2 rows × 53 columns
CPU times: user 516 ms, sys: 49 ms, total: 565 ms Wall time: 3.69 s
%%time
layers_to_add = [
{
"input_gdf": roads_gdf,
"legend": "roads",
"color": "black",
},
{
"input_gdf": pois_gdf,
"legend": "POIs",
"style": "square",
"color": "blue",
"size": 9
},
]
my_map = EasyMapBokeh(
"My roads and POIs - from OsmGT (https://github.com/amauryval)",
layers=layers_to_add
)
show(my_map.figure)
CPU times: user 1.1 s, sys: 35 ms, total: 1.13 s Wall time: 1.13 s
%%time
roads_topology_gdfs = roads_initialized.topology_checker()
lines_unchanged = roads_topology_gdfs["lines_unchanged"]
lines_added = roads_topology_gdfs["lines_added"]
lines_split = roads_topology_gdfs["lines_split"]
nodes_added = roads_topology_gdfs["nodes_added"]
intersections_added = roads_topology_gdfs["intersections_added"]
layers_to_add = [
{
"input_gdf": lines_unchanged,
"legend": "roads unchanged",
"color": "black",
},
{
"input_gdf": lines_added,
"legend": "roads added",
"color": "green",
},
{
"input_gdf": lines_split,
"legend": "roads split",
"color": "orange",
},
{
"input_gdf": intersections_added,
"legend": "Intersections added",
"color": "brown",
},
{
"input_gdf": nodes_added,
"legend": "Nodes added", # POIs here
"style": "square",
"color": "blue",
"size": 9
},
]
my_map = EasyMapBokeh(
"Topology about my roads and POIs - from OsmGT (https://github.com/amauryval)",
layers=layers_to_add
)
show(my_map.figure)
2020-10-01 22:08:45 - OsmGtRoads - INFO : Prepare topology data 2020-10-01 22:08:45 - OsmGtRoads - INFO : GeoDataframe Ready
CPU times: user 1.69 s, sys: 10.9 ms, total: 1.7 s Wall time: 1.69 s
%%time
graph = roads_initialized.get_graph()
# a plot method has been added on OsmGT.
graph.plot()
2020-10-01 22:08:46 - OsmGtRoads - INFO : Prepare graph 2020-10-01 22:08:47 - OsmGtRoads - INFO : Graph to image
CPU times: user 7.41 s, sys: 42.2 ms, total: 7.46 s Wall time: 4.79 s
%%time
# now, we have to define a start point and a end point and get their wkt
start_node_topo_uuid = 47
end_node_topo_uuid = 63
# 'topo_uuid' is generated by osmgt (during the topology processing).
# Some roads has been split that's whyso this id has been created.
start_node_gdf = pois_gdf[pois_gdf['topo_uuid'] == start_node_topo_uuid]
end_node_gdf = pois_gdf[pois_gdf['topo_uuid'] == end_node_topo_uuid]
start_node_wkt = start_node_gdf.iloc[0]["geometry"].wkt
end_node_wkt = end_node_gdf.iloc[0]["geometry"].wkt
# the graph have some methods (graph-tools method always exists!) to find egdes, vertices... Let's use the .find_vertex_from_name(). the wkt is the vertex name...
source_vertex = graph.find_vertex_from_name(start_node_wkt)
target_vertex = graph.find_vertex_from_name(end_node_wkt)
# shortest path computing...
path_vertices, path_edges = shortest_path(
graph,
source=source_vertex,
target=target_vertex,
weights=graph.edge_weights # weigth is based on line length
)
# get path by using edge names
roads_ids = [
graph.edge_names[edge]
for edge in path_edges
]
roads_gdf_copy = roads_gdf.copy(deep=True)
shortest_path_found = roads_gdf_copy[roads_gdf['topo_uuid'].isin(roads_ids)].to_crs(3857)['geometry'].to_list()
shortest_path_found_gdf = gpd.GeoDataFrame(index=[0], crs="EPSG:3857", geometry=[linemerge(shortest_path_found)])
layers_to_add = [
{
"input_gdf": shortest_path_found_gdf,
"legend": "shortest_path",
"color": "red",
"line_width": 5
},
{
"input_gdf": start_node_gdf,
"legend": "source node",
"color": "blue",
"style": "circle",
"size": 9
},
{
"input_gdf": end_node_gdf,
"legend": "target node",
"color": "green",
"style": "circle",
"size": 9
},
]
my_shortest_path_map = EasyMapBokeh(
"My shortest path - from OsmGT (https://github.com/amauryval)",
layers=layers_to_add
)
show(my_shortest_path_map.figure)
CPU times: user 160 ms, sys: 0 ns, total: 160 ms Wall time: 161 ms
%%time
start_node_topo_uuid = 47
end_node_topo_uuid = 63
start_node_gdf = pois_gdf[pois_gdf['topo_uuid'] == start_node_topo_uuid]
end_node_gdf = pois_gdf[pois_gdf['topo_uuid'] == end_node_topo_uuid]
start_node = start_node_gdf.iloc[0]["geometry"]
end_node = end_node_gdf.iloc[0]["geometry"]
shortest_paths = OsmGt.shortest_path_from_location(
"Roanne",
[
(start_node, end_node),
(start_node, end_node), # duplicate pairs are cleaned
],
mode="pedestrian"
)
layers_to_add = [
{
"input_gdf": shortest_paths[["geometry"]],
"legend": "shortest_path",
"color": "red",
"line_width": 5
},
{
"input_gdf": start_node_gdf,
"legend": "source node",
"color": "blue",
"style": "circle",
"size": 9
},
{
"input_gdf": end_node_gdf,
"legend": "target node",
"color": "green",
"style": "circle",
"size": 9
},
]
my_shortest_path_map = EasyMapBokeh(
"My shortest path - from OsmGT (https://github.com/amauryval)",
layers=layers_to_add
)
show(my_shortest_path_map.figure)
display(shortest_paths)
2020-10-01 22:08:51 - OsmGtShortestPath - INFO : Shortest path processing... 2020-10-01 22:08:51 - OsmGtShortestPath - INFO : From location: Roanne 2020-10-01 22:08:51 - OsmGtShortestPath - INFO : Loading data... 2020-10-01 22:08:52 - OsmGtShortestPath - INFO : NominatimApi: Query 200:OK in 0.36 sec. 2020-10-01 22:08:55 - OsmGtShortestPath - INFO : OverpassApi: Query 200:OK in 2.7 sec. 2020-10-01 22:08:55 - OsmGtShortestPath - INFO : Rebuild network data 2020-10-01 22:08:55 - OsmGtShortestPath - INFO : Network cleaning... 2020-10-01 22:08:55 - OsmGtShortestPath - INFO : Starting: Adding new nodes on the network 2020-10-01 22:08:55 - OsmGtShortestPath - INFO : Find nearest line for each node 2020-10-01 22:08:55 - OsmGtShortestPath - INFO : Split line 2020-10-01 22:08:55 - OsmGtShortestPath - INFO : Topology lines checker: to add: 2, to split: 2 2020-10-01 22:08:55 - OsmGtShortestPath - INFO : Starting: Find intersections 2020-10-01 22:08:55 - OsmGtShortestPath - INFO : Done: Find intersections 2020-10-01 22:08:55 - OsmGtShortestPath - INFO : Build lines 2020-10-01 22:08:55 - OsmGtShortestPath - INFO : Prepare graph 2020-10-01 22:08:55 - OsmGtShortestPath - INFO : Prepare GeoDataframe 2020-10-01 22:08:55 - OsmGtShortestPath - INFO : GeoDataframe Ready 2020-10-01 22:08:55 - OsmGtShortestPath - INFO : Compute shortest path from 2189 to 2188 2020-10-01 22:08:55 - OsmGtShortestPath - INFO : Prepare GeoDataframe 2020-10-01 22:08:55 - OsmGtShortestPath - INFO : GeoDataframe Ready 2020-10-01 22:08:55 - OsmGtShortestPath - INFO : Prepare GeoDataframe 2020-10-01 22:08:55 - OsmGtShortestPath - INFO : GeoDataframe Ready
| source_node | target_node | osm_ids | osm_urls | geometry | id | |
|---|---|---|---|---|---|---|
| 0 | POINT (4.0697088 46.0410178) | POINT (4.0757785 46.0315038) | 99078739, 99078739, 99078739, 99211707, 992117... | https://www.openstreetmap.org/way/99078739, ht... | LINESTRING (4.06971 46.04102, 4.06966 46.04086... | 0 |
CPU times: user 824 ms, sys: 16 ms, total: 840 ms Wall time: 4.15 s
%%time
start_node_topo_uuid = 47
source_node = pois_gdf[pois_gdf['topo_uuid'] == start_node_topo_uuid]
# 2 = 2 min ; 5 = 5 min ; 10 = 10 min
isochrones_polygon_values = {
2: "#d9ef8b",
5: "#fee08b",
10: "#f46d43"
}
isochrones_lines_values = {
2: "#668100",
5: "#e2a803",
10: "#962603"
}
trip_speed = 3 # km/h
location_point = source_node.iloc[0]["geometry"]
isochrones_polygons, isochrones_lines = OsmGt.isochrone_from_source_node(
location_point,
list(isochrones_polygon_values.keys()),
trip_speed,
mode="pedestrian"
)
isochrones_polygons["color"] = isochrones_polygons["iso_name"].map(lambda x: isochrones_polygon_values[x])
isochrones_lines["color"] = isochrones_lines["iso_name"].map(lambda x: isochrones_lines_values[x])
layers_to_add = [
{
"input_gdf": isochrones_polygons,
"legend": "iso_name",
"fill_color": "color",
"line_color": "color",
"fill_alpha": 0.5
},
{
"input_gdf": source_node,
"legend": "Source node",
"style": "circle",
"color": "red",
"size": 5
},
{
"input_gdf": isochrones_lines,
"legend": "iso_name",
"color": "color",
"line_width": 2
},
]
my_shortest_path_map = EasyMapBokeh(
"Isochrones from times - from OsmGT (https://github.com/amauryval)",
layers=layers_to_add
)
show(my_shortest_path_map.figure)
print("\nIsochrones polygons output")
display(isochrones_polygons)
print("\nIsochrones lines output")
display(isochrones_lines.head(2))
2020-10-01 22:08:55 - OsmGtIsochrone - INFO : Isochrone processing... 2020-10-01 22:08:56 - OsmGtIsochrone - INFO : From bbox: (4.063869750653223, 46.03696451481586, 4.075547849346776, 46.04507078784993) 2020-10-01 22:08:56 - OsmGtIsochrone - INFO : Loading data... 2020-10-01 22:08:56 - OsmGtIsochrone - INFO : OverpassApi: Query 200:OK in 0.82 sec. 2020-10-01 22:08:56 - OsmGtIsochrone - INFO : Rebuild network data 2020-10-01 22:08:56 - OsmGtIsochrone - INFO : Network cleaning... 2020-10-01 22:08:56 - OsmGtIsochrone - INFO : Starting: Adding new nodes on the network 2020-10-01 22:08:56 - OsmGtIsochrone - INFO : Find nearest line for each node 2020-10-01 22:08:56 - OsmGtIsochrone - INFO : Split line 2020-10-01 22:08:56 - OsmGtIsochrone - INFO : Topology lines checker: to add: 1, to split: 1 2020-10-01 22:08:56 - OsmGtIsochrone - INFO : Starting: Find intersections 2020-10-01 22:08:56 - OsmGtIsochrone - INFO : Done: Find intersections 2020-10-01 22:08:56 - OsmGtIsochrone - INFO : Build lines 2020-10-01 22:08:59 - OsmGtIsochrone - INFO : OverpassApi: Query 200:OK in 2.1 sec. 2020-10-01 22:08:59 - OsmGtIsochrone - INFO : Prepare GeoDataframe 2020-10-01 22:08:59 - OsmGtIsochrone - INFO : GeoDataframe Ready 2020-10-01 22:08:59 - OsmGtIsochrone - INFO : Prepare graph 2020-10-01 22:08:59 - OsmGtIsochrone - INFO : Compute isochrone: 10 minutes => 500 meters 2020-10-01 22:08:59 - OsmGtIsochrone - INFO : Compute isochrone: 5 minutes => 250 meters 2020-10-01 22:08:59 - OsmGtIsochrone - INFO : Compute isochrone: 2 minutes => 100 meters 2020-10-01 22:09:01 - OsmGtIsochrone - INFO : GeoDataframe Ready
ERROR:bokeh.core.validation.check:E-1006 (NON_MATCHING_DATA_SOURCES_ON_LEGEND_ITEM_RENDERERS): LegendItem.label is a field, but renderer data sources don't match: LegendItem(id='1824', ...)
Isochrones polygons output
| iso_name | time_unit | iso_distance | distance_unit | geometry | id | color | |
|---|---|---|---|---|---|---|---|
| 0 | 10 | minutes | 500 | meters | POLYGON ((4.07179 46.04027, 4.07179 46.04027, ... | 0 | #f46d43 |
| 1 | 10 | minutes | 500 | meters | POLYGON ((4.06439 46.04167, 4.06498 46.04153, ... | 1 | #f46d43 |
| 2 | 5 | minutes | 250 | meters | POLYGON ((4.06947 46.04000, 4.06947 46.04000, ... | 2 | #fee08b |
| 3 | 2 | minutes | 100 | meters | POLYGON ((4.06863 46.04104, 4.06864 46.04106, ... | 3 | #d9ef8b |
Isochrones lines output
| highway | junction | name | id | osm_url | topo_uuid | topology | maxspeed | oneway | source | ... | foot | horse | EntityHand | SubClasses | ref:FR:FANTOIR | maxspeed:type | iso_name | iso_distance | geometry | color | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | pedestrian | NaN | Passage des Bains | 357803019 | https://www.openstreetmap.org/way/357803019 | 1011 | unchanged | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | 10.0 | 500.0 | LINESTRING (4.06532 46.03856, 4.06544 46.03862... | #962603 |
| 3 | secondary | NaN | Place Louis Flandre | 375987158 | https://www.openstreetmap.org/way/375987158 | 1023 | unchanged | 50 | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | 10.0 | 500.0 | LINESTRING (4.07277 46.04221, 4.07281 46.04217... | #962603 |
2 rows × 33 columns
CPU times: user 3.36 s, sys: 177 ms, total: 3.54 s Wall time: 6.2 s
%%time
start_node_topo_uuid = 47 # change the value here to define a new source point to compute isochrones
source_node = pois_gdf[pois_gdf['topo_uuid'] == start_node_topo_uuid]
trip_speed = 3 # km/h
isochrones_polygon_values = {
250: "#d9ef8b",
500: "#fee08b",
750: "#f46d43",
1000: "#8B4513"
}
# 250 = 250 meters ; 500 = 500 meters ; etc
isochrones_lines_values = {
250: "#668100",
500: "#e2a803",
750: "#962603",
1000: "#000000"
}
location_point = source_node.iloc[0]["geometry"]
isochrones_polygons, isochrones_lines = OsmGt.isochrone_distance_from_source_node(
location_point,
list(isochrones_polygon_values.keys()), # meters
trip_speed,
mode="pedestrian"
)
isochrones_polygons["color"] = isochrones_polygons["iso_distance"].map(lambda x: isochrones_polygon_values[x])
isochrones_lines["color"] = isochrones_lines["iso_distance"].map(lambda x: isochrones_lines_values[x])
layers_to_add = [
{
"input_gdf": isochrones_polygons,
"legend": "iso_distance",
"fill_color": "color",
"line_color": "color",
"fill_alpha": 0.5
},
{
"input_gdf": isochrones_lines,
"legend": "iso_distance",
"color": "color",
"line_width": 2
},
{
"input_gdf": source_node,
"legend": "Source node",
"style": "circle",
"color": "red",
"size": 15
},
]
my_shortest_path_map = EasyMapBokeh(
"Isochrones from distance - from OsmGT (https://github.com/amauryval)",
layers=layers_to_add
)
show(my_shortest_path_map.figure)
2020-10-01 22:09:02 - OsmGtIsochrone - INFO : Isochrone processing... 2020-10-01 22:09:02 - OsmGtIsochrone - INFO : From bbox: (4.058030701306445, 46.032910932296, 4.0813868986935535, 46.0491234783672) 2020-10-01 22:09:02 - OsmGtIsochrone - INFO : Loading data... 2020-10-01 22:09:03 - OsmGtIsochrone - INFO : OverpassApi: Query 200:OK in 1.32 sec. 2020-10-01 22:09:03 - OsmGtIsochrone - INFO : Rebuild network data 2020-10-01 22:09:03 - OsmGtIsochrone - INFO : Network cleaning... 2020-10-01 22:09:03 - OsmGtIsochrone - INFO : Starting: Adding new nodes on the network 2020-10-01 22:09:03 - OsmGtIsochrone - INFO : Find nearest line for each node 2020-10-01 22:09:03 - OsmGtIsochrone - INFO : Split line 2020-10-01 22:09:03 - OsmGtIsochrone - INFO : Topology lines checker: to add: 1, to split: 1 2020-10-01 22:09:03 - OsmGtIsochrone - INFO : Starting: Find intersections 2020-10-01 22:09:03 - OsmGtIsochrone - INFO : Done: Find intersections 2020-10-01 22:09:03 - OsmGtIsochrone - INFO : Build lines 2020-10-01 22:09:07 - OsmGtIsochrone - INFO : OverpassApi: Query 200:OK in 3.31 sec. 2020-10-01 22:09:07 - OsmGtIsochrone - INFO : Prepare GeoDataframe 2020-10-01 22:09:07 - OsmGtIsochrone - INFO : GeoDataframe Ready 2020-10-01 22:09:07 - OsmGtIsochrone - INFO : Prepare graph 2020-10-01 22:09:10 - OsmGtIsochrone - INFO : Compute isochrone: 20.0 minutes => 1000 meters 2020-10-01 22:09:10 - OsmGtIsochrone - INFO : Compute isochrone: 15.0 minutes => 750 meters 2020-10-01 22:09:10 - OsmGtIsochrone - INFO : Compute isochrone: 10.0 minutes => 500 meters 2020-10-01 22:09:10 - OsmGtIsochrone - INFO : Compute isochrone: 5.0 minutes => 250 meters 2020-10-01 22:09:17 - OsmGtIsochrone - INFO : GeoDataframe Ready
ERROR:bokeh.core.validation.check:E-1006 (NON_MATCHING_DATA_SOURCES_ON_LEGEND_ITEM_RENDERERS): LegendItem.label is a field, but renderer data sources don't match: LegendItem(id='2036', ...)
CPU times: user 13.2 s, sys: 90.8 ms, total: 13.3 s Wall time: 15.6 s